package by.ostrovsky.webhousingsystem.controller.validation;

import org.apache.log4j.Logger;

import by.ostrovsky.webhousingsystem.controller.ICheckController;
import by.ostrovsky.webhousingsystem.dao.DAOFactory;
import by.ostrovsky.webhousingsystem.dao.implementation.idao.IReadDAO;
import by.ostrovsky.webhousingsystem.exception.CommonException;
import by.ostrovsky.webhousingsystem.exception.OperationErrorException;
import by.ostrovsky.webhousingsystem.hibernate.entity.Account;

public class LoginCheckInController implements ICheckController<Account, Boolean>{
	private static Logger logger = Logger
			.getLogger(LoginCheckInController.class);

	@Override
	public Boolean check(Account account) throws CommonException {
		try {
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
			IReadDAO<Account> dao = factory.getReadAccountDAO();

			Account acc = dao.read(account);
			
			Boolean match = acc.getId().equals(account.getId());
			if (match)
				return false;
		} catch (CommonException e) {
			logger.info(e);
			throw new OperationErrorException();
		}
		catch(NullPointerException e){
			return true;
		}
		return true;
	}
}
